<template>
	<view>
		<view style="margin-top: 20rpx;margin-left: 20rpx;font-weight: bold;">标题:</view>
		<view class="titlebox">
			<input class="title" placeholder="取个标题吧!" v-model="title" type="text"></input>
		</view>
		<view style="margin-top: 20rpx;margin-left: 20rpx;font-weight: bold;">内容:</view>
		<view class="contentbox">
			<textarea class="content" v-model="content" placeholder="说点什么吧!"></textarea>
		</view>
		<view class="imagebox">
			<view class="title">添加图片：</view>
			<view class="info">{{imgUrl.length}}/9</view>
		</view>
		<view class="addImage-box">
			<view class="addImage-item" v-for="(img,index) in imgUrl" :key="index">
				<view class="close-icon" @click="del(index)">
					<uni-icons type="closeempty" size="24" color="#fff"></uni-icons>
				</view>
				<view class="image-box">
					<image :src="img" mode="aspectFill" @click="previewImg(img)"></image>
				</view>
			</view>
		
			<view v-if="imageList.length < 9" class="addImage-item" @click="chooseImage">
				<view class="image-box">
					<uni-icons type="plusempty" size="70" color="#999"></uni-icons>
				</view>
			</view>
		</view>
		<view class="telnumber">
			<view class="teltext">目标院校：</view>
			<view class="tel">
				<picker @change="choseuniversity" :value="index2" :range="universityarray">
					<view class="number" style="color: #333333;">{{universityarray[index2]?universityarray[index2]:"选择院校"}}</view>
				</picker>
			</view>
		</view>
		<view class="telnumber">
			<view class="teltext">目标专业：</view>
			<view class="tel">
				<picker @change="chosemajor" :value="number" :range="majorarray">
					<view class="number">{{majorarray[number]?majorarray[number]:"选择专业"}}</view>
				</picker>
			</view>
		</view>
		<button @click="createpost" class="button">发布</button>
	</view>
</template>

<script>
	export default{
		onLoad() {
			var userId = getApp().globalData.userId
			this.userId = getApp().globalData.userId
			console.log(this.userId)
		},
		data() {
			return {
				userId: "",
				content:"",
				title:"",
				imgUrl: [], //模拟上传后从后台获得的图片和视频
				enableDel: true,
				enableAdd: true,
				shoopimg: '',
				shooptitle: '',
				imageList: [], //图片
				texthid: true,
				direction: "up",
				groupList: [],
				inputtext: "",
				searchList: [],
				issend: false,
				university: "",
				major: "",
				number:null,
				index2:null,
				picId: "",
				universityarray: ['清华大学', '北京大学', '中山大学','浙江工商大学','南京大学', '同济大学', '浙江大学', '上海交通大学'],
				majorarray: ['计算机技术','软件工程','人工智能','金融学', '经济学']
			}
		},
		methods:{
			previewImg(url) { //预览图片
				let arr = []
				arr[0] = url
				uni.previewImage({
					urls: arr
				})
			},
			
			createpost(){
				console.log(this.title + "     "+this.content+" "+this.university+" "+this.major)
				uni.request({
				    url: this.baseurl + '/releaseTopic',
					method: "POST",
					header: {
						'content-type': "application/json",
					},
					data: {
						"title": this.title,
						"creator": this.userId,
						"groupId": this.index2 + 1,
						"content": this.content,
						"picId": this.picId,
						"tag": this.university + "|" + this.major,
						"school": this.university,
						"major": this.major
					},
				    success: (res) => {
						console.log(res.data);
						console.log("success");
						uni.showToast({
							title: "发送成功",
							icon: 'none',
							duration: 2000,
						});
						setTimeout(() => {
							uni.navigateBack({
							});
						}, 1000);
				    },
				})
			},
			del(index) {
				this.imgUrl.splice(index, 1)
			},
			chooseImage() {
				let that = this;
				uni.chooseImage({
					//该方法是调出选择图片的方法
					count: 9, //数量限制
					sizeType: ['original', 'compressed'], //可选 原图 或缩略图
					success: function(res) {
						console.log(res);
						const tempFiles = res.tempFiles
						let imgs = tempFiles.map((value, index)=> {
							return {
								name: "files", 
								uri: value.path
							}
						})
						console.log(imgs);
						uni.uploadFile({
							url: that.baseurl + "/uploadPicture",
							files: imgs,
							success: (uploadFileRes) => {
								console.log(uploadFileRes);
								var uploadImgObj = JSON.parse(uploadFileRes.data);
								console.log(uploadImgObj);
								console.log(uploadImgObj.picurls);
								for (var i = 0; i < uploadImgObj.picurls.length; i++) {
									that.imgUrl.push(uploadImgObj.picurls[i])
									that.picId = that.picId + uploadImgObj.picids[i] + "|"
								}
							}
						})
					}
				});
			},
			choseuniversity: function(e) {
				//this.university = this.universityarray[e.target.value]
				this.index2 = e.target.value
				this.university = this.universityarray[e.target.value]
				console.log(this.university)
			},
			chosemajor: function(e) {
				//this.university = this.universityarray[e.target.value]
				this.number = e.target.value
				this.major = this.majorarray[e.target.value]
				console.log(this.major)
			},
			
		}
	}
</script>

<style lang="scss">
	.titlebox{
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #F8F8F8;
		width: 710rpx;
		height: 80rpx;
		margin-top: 20rpx;
		margin-left: 20rpx;
		border-radius: 20rpx;
		
		.title{
			width: 680rpx;
			//background-color: #2C405A;
			//padding: 20rpx;
		}
	}
	
	.contentbox{
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #F8F8F8;
		width: 710rpx;
		height:300rpx;
		margin-top: 20rpx;
		margin-left: 20rpx;
		border-radius: 20rpx;
		
		.content{
			width: 680rpx;
			//background-color: #007AFF;
		}
	}
	
	.imagebox {
		//background-color: #007AFF;
		display: flex;
		flex-direction: row;
		width: 100%;
		//height: 300rpx;
		margin-top: 40rpx;
		justify-content: space-between;
	
		.title {
			color: #000000;
			font-size: 45rpx;
			font-weight: bold;
			margin-left: 20rpx;
		}
	
		.info {
			color: #797979;
			display: flex;
			justify-content: center;
			align-items: center;
			//background-color: #007AFF;
			margin-right: 20rpx;
		}
	}
	
	.addImage-box {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx;
		//background-color: #0000FF;
	
		.addImage-item {
			position: relative;
			width: 33.33%;
			height: 0;
			padding-top: 33.33%;
			//box-sizing: border-box;
	
			.close-icon {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				right: 10rpx;
				top: 10rpx;
				z-index: 2;
				width: 22px;
				height: 22px;
				border-radius: 10rpx;
				background-color: #9f9f9f;
			}
	
			.image-box {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 10rpx;
				bottom: 10rpx;
				left: 10rpx;
				right: 10rpx;
				border: 1px solid #eee;
				border-radius: 10rpx;
				overflow: hidden;
	
				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
	}
	
	.telnumber {
		display: flex;
		flex-direction: row;
		align-items: center;
		height: 100rpx;
		//width: 600rpx;
		//background-color: #333333;
		justify-content: flex-start;
		//margin-right: 30rpx;
	
		.teltext {
			margin-left: 20rpx;
			font-weight: bold;
			//margin-right: 20rpx;
		}
	
		.tel {
			display: flex;
			align-items: center;
			border-radius: 10rpx;
			height: 70rpx;
			width: 550rpx;
			background-color: #F8F8F8;
	
			.number {
				//background-color: #007AFF;
				width: 500rpx;
				padding-left: 10rpx;
			}
		}
	}
	
	.button{
		margin-top: 20rpx;
		font-weight: bold;
		color: #FFFFFF;
		background-color: #007AFF;
		width: 150rpx;
	}
</style>
